package com.bf.electroplating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.exhaustGasWarning.ExhaustGasWarningHandleDTO;
import com.bf.electroplating.pojo.vo.exhaustGasWarning.ExhaustGasWarningQueryDTO;
import com.bf.electroplating.pojo.vo.exhaustGasWarning.ExhaustGasWarningStatsVO;
import com.bf.electroplating.pojo.vo.exhaustGasWarning.ExhaustGasWarningVO;
import com.bf.electroplating.service.IExhaustGasWarningService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;

/**
 * 废气处理设施监控预警控制器
 */
@Slf4j
@RestController
@RequestMapping("/exhaust-gas-warning")
@Tag(name = "废气处理设施监控预警", description = "废气处理设施监控预警相关接口")
@Validated
public class ExhaustGasWarningController {

    @Autowired
    private IExhaustGasWarningService exhaustGasWarningService;

    @PostMapping("/pageList")
    @Operation(summary = "分页查询废气预警", description = "分页查询废气处理设施监控预警列表")
    public ResultVO<Page<ExhaustGasWarningVO>> pageList(@Valid @RequestBody ExhaustGasWarningQueryDTO queryDTO) {
        log.info("分页查询废气预警，查询条件：{}", queryDTO);
        
        try {
            Page<ExhaustGasWarningVO> result = exhaustGasWarningService.pageList(queryDTO);
            return ResultVO.ok(result);
        } catch (Exception e) {
            log.error("分页查询废气预警失败", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取废气预警详情", description = "根据ID获取废气预警详细信息")
    public ResultVO<ExhaustGasWarningVO> getDetail(
            @Parameter(description = "预警ID", required = true)
            @PathVariable @NotBlank(message = "ID不能为空") String id) {
        log.info("获取废气预警详情，ID：{}", id);
        
        try {
            ExhaustGasWarningVO result = exhaustGasWarningService.getDetail(id);
            if (result == null) {
                return ResultVO.fail("数据不存在");
            }
            return ResultVO.ok(result);
        } catch (Exception e) {
            log.error("获取废气预警详情失败", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/handle")
    @Operation(summary = "处置废气预警", description = "处置单个废气预警")
    public ResultVO<Boolean> handleWarning(@Valid @RequestBody ExhaustGasWarningHandleDTO handleDTO) {
        log.info("处置废气预警，数据：{}", handleDTO);
        
        try {
            Boolean result = exhaustGasWarningService.handleWarning(handleDTO);
            if (result) {
                return ResultVO.ok(true, "处置成功");
            } else {
                return ResultVO.fail("处置失败");
            }
        } catch (Exception e) {
            log.error("处置废气预警失败", e);
            return ResultVO.fail("处置失败：" + e.getMessage());
        }
    }






}
